home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / origami / writefolds.c < prev   
Encoding:
C/C++ Source or Header  |  1996-09-27  |  11.3 KB  |  434 lines

  1. /*{{{}}}*/
  2. /*{{{  #includes*/
  3. #ifdef CONFIG_H
  4. #   include "config.h"
  5. #endif
  6.  
  7. #include <sys/types.h>
  8. #include <ctype.h>
  9. #include <limits.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14.  
  15. #define WRITEFOLDS_C
  16. #define I_LOOP_C
  17. #define I_DISPLAY_C
  18. #define I_FOLDING_C
  19. #define I_FOLDHELP_C
  20. #define I_GETMSG_C
  21. #define I_GETTK_C
  22. #define I_KEYBOARD_C
  23. #define I_MAIN_C
  24. #define I_MESSAGES_C
  25. #define I_ORIEDT_C
  26. #define I_PROMPT_C
  27. #define I_READFOLDS_C
  28. #define I_VIRTUAL_C
  29.  
  30. #include "origami.h"
  31. #include <lib/ori_add_lib.h>
  32. /*}}}  */
  33.  
  34. /*{{{  variables*/
  35. private element const *wf_pn;
  36. private element const *wf_p;
  37. private unsigned char *wf_dsp_line;
  38. private FILE *wf_tdsfile;
  39. public int current_ind;
  40. /*}}}  */
  41.  
  42. /*{{{  write_fold*/
  43. /*{{{  move_on_full_wf_pn*/
  44. private void move_on_full_wf_pn(void)
  45. {
  46.   if (test_linetyp(*wf_pn,(START_FOLD|START_FILED)))
  47.     wf_pn = wf_pn->x.fold.data;
  48.   else
  49.     wf_pn = wf_pn->next;
  50. }
  51. /*}}}  */
  52. /*{{{  write_tds*/
  53. /*{{{  write_tag*/
  54. private void write_tag(int tag,int value)
  55. {
  56.   if (value > 63)
  57.    { fputc((unsigned)(((int)value) >> 6) + 192,wf_tdsfile);
  58.      file_io_size.c++;
  59.    }
  60.  
  61.   fputc((int)((tag << 6) + (value & 63)),wf_tdsfile);
  62.   file_io_size.c++;
  63. }
  64. /*}}}  */
  65. /*{{{  write_string*/
  66. private void write_string(unsigned char const * const s)
  67. { int l=ustrlen(s);
  68.  
  69.   write_tag(0,l);
  70.   fwrite(s,(size_t)1,(size_t)l,wf_tdsfile);
  71.   file_io_size.c+=l;
  72. }
  73. /*}}}  */
  74. /*{{{  write_header*/
  75. private void write_header(void)
  76. {
  77.   write_tag(2, 0);   /*startlist*/
  78.   write_string(wf_dsp_line);
  79.   write_tag(1, (int)wf_p->x.fold.UU.U1.type);
  80.   write_tag(1, (int)wf_p->x.fold.UU.U1.contents);
  81.   write_tag(1, (int)wf_p->x.fold.UU.U1.indent);
  82.   write_tag(2, 1);   /*endlist*/
  83. }
  84. /*}}}  */
  85. /*{{{  write_a_list*/
  86. private void write_a_list(void)
  87. {
  88.   linetyp lt;
  89.  
  90.   write_tag(2, 0);
  91.   while ((lt=get_linetyp(*wf_pn)) != END_FOLD)
  92.    { if (aborted)
  93.         return;
  94.      wf_p = wf_pn;
  95.      move_on_full_wf_pn();
  96.      ustrcpy(wf_dsp_line,get_data(wf_p));
  97.      trailing_spaces(wf_dsp_line);
  98.      switch (lt)
  99.       { case NOT_FOLD:
  100.            write_string(wf_dsp_line);
  101.            file_io_size.l++;
  102.         default:
  103.            break;
  104.         case START_FOLD:
  105.         case START_OPEN_FOLD:
  106.            file_io_size.l+=2;
  107.            write_tag(2, 2);
  108.            write_header();
  109.            write_a_list();
  110.            write_tag(2, 3);   /*endfold*/
  111.            continue;
  112.         case START_FILED:
  113.            file_io_size.l+=3;
  114.            write_tag(2, 4);
  115.            write_header();
  116.            write_string(get_data(wf_p->x.fold.other_end));
  117.            write_tag(2, 5);   /*endfiled*/
  118.            move_on_full_wf_pn();
  119.            continue;
  120.       }
  121.    }
  122.   write_tag(2, 1);
  123.   move_on_full_wf_pn();
  124. }
  125. /*}}}  */
  126.  
  127. private boolean write_tds(unsigned char const * const filename)
  128. {
  129.   /*{{{  open tds-file*/
  130.   wf_tdsfile = fopen((char *)filename,(char*)"wb");
  131.   if (wf_tdsfile == (FILE*)0)
  132.    { err_message(M_CANTWRITE,filename);
  133.      return(False);
  134.    }
  135.   /*}}}  */
  136.   write_a_list();
  137.   /*{{{  close tds-file*/
  138.   if (fclose(wf_tdsfile)<0)
  139.    { err_message(M_CLOSE_FAILED,filename);
  140.      return(False);
  141.    }
  142.   /*}}}  */
  143.   message
  144.    ( get_msg
  145.       ( F_SIZE_FILE,
  146.         filename,
  147.         "wrote",
  148.         file_io_size.l,
  149.         file_io_size.c
  150.       )
  151.    );
  152.   wf_tdsfile = 0;
  153.   return(!aborted);
  154. }
  155. /*}}}  */
  156. /*{{{  w_lang_mark*/
  157. private void w_lang_mark(FILE * const tstorefile)
  158. {
  159.   unsigned char *m;
  160.  
  161.   m=get_msg
  162.      ( MSG_ARG_FORMAT,
  163.        "%s%s%s%s\n",
  164.        dialects[bd.m.dialect.typ].txt.start,
  165.        bd.f.str.open_f,
  166.        bd.f.str.close_f,
  167.        dialects[bd.m.dialect.typ].txt.end
  168.     );
  169.   fputs((char*)m,tstorefile);
  170.   file_io_size.c+=ustrlen(m);
  171.   file_io_size.l++;
  172. }
  173. /*}}}  */
  174.  
  175. public boolean write_fold
  176.  ( unsigned char const * const filename,
  177.    element const * const from_ptr,
  178.    FILE * const write_pipe
  179.  )
  180. {
  181.   /*{{{  variables*/
  182.   unsigned char dsp_line[LINELEN + 1];
  183.   FILE *tstorefile;
  184.   /*}}}  */
  185.  
  186.   /*{{{  init writing*/
  187.   wf_dsp_line=dsp_line;
  188.   wf_pn = from_ptr;
  189.   wf_pn=move_on(wf_pn);
  190.   current_ind = 0;
  191.   file_io_size.c=0;
  192.   file_io_size.l=0;
  193.   /*}}}  */
  194.   /*{{{  if not pipe, check dir and show write string*/
  195.   if (write_pipe==(FILE*)0)
  196.      if (is_dir((FILE*)0,(char*)filename))
  197.       { msg_message(M_DIR_EDIT);
  198.         return(False);
  199.       }
  200.      else
  201.         message(get_msg(F_WRITING,filename));
  202.   /*}}}  */
  203.   if (bd.m.dialect.typ != F_C_TDS)
  204.    /*{{{  ascii-types*/
  205.    { boolean mark_comments=(bd.m.dialect.typ!=F_C_NONE) && !no_fold_out;
  206.  
  207.      /*{{{  open tstorefile*/
  208.      if (write_pipe)
  209.        tstorefile=write_pipe;
  210.      else
  211.        tstorefile = fopen((char *)filename,(char*)"w");
  212.      if (tstorefile==(FILE*)0)
  213.       { err_message(M_CANTWRITE,filename);
  214.         return(False);
  215.       }
  216.      if (is_dir(wf_tdsfile,(char*)0)) {
  217.        fclose(wf_tdsfile);
  218.        msg_message(M_DIR_EDIT);
  219.        return(False);
  220.      }
  221.      /*}}}  */
  222.      if (!executing_macro) enable_abort();
  223.      while (wf_pn != from_ptr->x.fold.other_end && !aborted)    /*tail*/
  224.       /*{{{  handle one line*/
  225.       { linetyp lt;
  226.  
  227.         wf_p = wf_pn;
  228.         lt=get_linetyp(*wf_p);
  229.         /*{{{  get text data for current line and move #*/
  230.         { unsigned char *s;
  231.  
  232.           s=wf_dsp_line;
  233.           /*{{{  get line data*/
  234.           if (current_ind>0)
  235.            { spaces(wf_dsp_line,current_ind);
  236.              copyin(s+=current_ind,wf_p,True);
  237.              if (!*s)
  238.               { wf_dsp_line[0]='\0';
  239.                 goto no_hash_shift;
  240.               }
  241.            }
  242.           else
  243.              copyin(s,wf_p,True);
  244.           /*}}}  */
  245.           if (bd.m.hash_shift)
  246.            /*{{{  move #*/
  247.            { for (;;s++)
  248.               { switch (*s)
  249.                  { case '#':
  250.                       *s=' ';
  251.                       *wf_dsp_line='#';
  252.                    default:
  253.                       break;
  254.                    case ' ':
  255.                       continue;
  256.                  }
  257.                 break;
  258.               }
  259.            }
  260.            /*}}}  */
  261.           no_hash_shift:;
  262.         }
  263.         /*}}}  */
  264.         /*{{{  maybe print language-mark-line*/
  265.         if (mark_comments && lt!=NOT_FOLD) {
  266.           int i;
  267.  
  268.           for
  269.            ( i=(lt&(START_FOLD|START_FILED))
  270.                 ? wf_p->indent+wf_p->x.fold.UU.U1.indent
  271.                 : current_ind+wf_p->indent
  272.            ; i--
  273.            ; file_io_size.c++
  274.            )
  275.              fputc(' ',tstorefile);
  276.           w_lang_mark(tstorefile);
  277.           mark_comments=False;
  278.         }
  279.         /*}}}  */
  280.         /*{{{  write the line*/
  281.         fputs((char *)wf_dsp_line,tstorefile);
  282.         fputc('\n',tstorefile);
  283.         /*}}}  */
  284.         /*{{{  count chars and lines*/
  285.         file_io_size.l++;
  286.         file_io_size.c+=ustrlen(wf_dsp_line)+1;
  287.         /*}}}  */
  288.         switch (lt)
  289.          { case START_FOLD:
  290.            case START_FILED:
  291.               current_ind+=wf_p->indent+wf_p->x.fold.UU.U1.indent;
  292.            case START_OPEN_FOLD:
  293.             /*{{{  handle additional informations*/
  294.             { boolean norm_att_f;
  295.  
  296.               /*{{{  generate correct fold-file-string*/
  297.               spaces(wf_dsp_line,current_ind);
  298.               ustrcat(wf_dsp_line,dialects[bd.m.dialect.typ].txt.start);
  299.               ustrcat(wf_dsp_line,bd.f.str.file_f);
  300.               /*}}}  */
  301.               norm_att_f=normal_att(&(wf_p->x.fold),False);
  302.               if (!norm_att_f)
  303.                /*{{{  write the tags*/
  304.                { unsigned char *a;
  305.  
  306.                  a=get_msg
  307.                     ( MSG_ARG_FORMAT,
  308.                       "%s%s%d %d %s%s\n",
  309.                       wf_dsp_line,
  310.                       fold_a,
  311.                       wf_p->x.fold.UU.U1.type,
  312.                       wf_p->x.fold.UU.U1.contents,
  313.                       get_data(wf_p->x.fold.other_end),
  314.                       dialects[bd.m.dialect.typ].txt.end
  315.                     );
  316.                  fputs((char*)a,tstorefile);
  317.                  /*{{{  count chars and lines*/
  318.                  file_io_size.c+=ustrlen(a);
  319.                  file_io_size.l++;
  320.                  /*}}}  */
  321.                }
  322.                /*}}}  */
  323.               else if (lt==START_FILED)
  324.                /*{{{  write the filename*/
  325.                { unsigned char *a;
  326.  
  327.                  a=get_msg
  328.                     ( MSG_ARG_FORMAT,
  329.                       "%s%s%s%s\n",
  330.                       wf_dsp_line,
  331.                       fold_f,
  332.                       get_data(wf_p->x.fold.other_end),
  333.                       dialects[bd.m.dialect.typ].txt.end
  334.                     );
  335.                  fputs((char*)a,tstorefile);
  336.                  /*{{{  count chars and lines*/
  337.                  file_io_size.c+=ustrlen(a);
  338.                  file_io_size.l++;
  339.                  /*}}}  */
  340.                  if (wf_p->x.fold.UU.U1.type == 1)
  341.                   /*{{{  handle missing close-fold and indent*/
  342.                   {
  343.                     /*{{{  if !normal_att, so write the close-fold line*/
  344.                     if (!norm_att_f)
  345.                      { if (current_ind)
  346.                         { int x=current_ind;
  347.  
  348.                           do
  349.                            { fputc(' ',tstorefile);
  350.                              file_io_size.c++;
  351.                            }
  352.                           while (--x>0);
  353.                         }
  354.                        a=get_msg
  355.                           ( MSG_ARG_FORMAT,
  356.                             "%s%s  %s\n",
  357.                             dialects[bd.m.dialect.typ].txt.start,
  358.                             bd.f.str.close_f,
  359.                             dialects[bd.m.dialect.typ].txt.end
  360.                           );
  361.                        fputs((char*)a,tstorefile);
  362.                        /*{{{  count chars and lines*/
  363.                        file_io_size.c+=ustrlen(a);
  364.                        file_io_size.l++;
  365.                        /*}}}  */
  366.                      }
  367.                     /*}}}  */
  368.                     current_ind-=wf_p->indent+wf_p->x.fold.UU.U1.indent;
  369.                     wf_pn = wf_p->x.fold.other_end;
  370.                   }
  371.                   /*}}}  */
  372.                }
  373.                /*}}}  */
  374.             }
  375.             /*}}}  */
  376.            default:
  377.               break;
  378.          }
  379.         move_on_full_wf_pn();
  380.         if (lt==END_FOLD)
  381.           if (test_linetyp(*(wf_p->x.fold.other_end),(START_FOLD|START_FILED)))
  382.             current_ind-=
  383.                  wf_p->x.fold.other_end->indent
  384.                + wf_p->x.fold.other_end->x.fold.UU.U1.indent;
  385.       }
  386.       /*}}}  */
  387.      if (!executing_macro) disable_abort();
  388.      /*{{{  maybe print language-mark-line*/
  389.      if (mark_comments) w_lang_mark(tstorefile);
  390.      /*}}}  */
  391.      /*{{{  show wrote message*/
  392.      message
  393.       ( get_msg
  394.          ( F_SIZE_FILE,
  395.            (write_pipe?M_PSTR:filename),
  396.            "wrote",
  397.            file_io_size.l,
  398.            file_io_size.c
  399.          )
  400.       );
  401.      /*}}}  */
  402.      /*{{{  maybe close tstorefile*/
  403.      if (write_pipe==(FILE*)0) {
  404.        if (fclose(tstorefile)<0)
  405.         { err_message(M_CLOSE_FAILED,filename);
  406.           return(False);
  407.         }
  408.      }
  409.      return(!aborted);
  410.      /*}}}  */
  411.    }
  412.    /*}}}  */
  413.   else
  414.     return(write_tds(filename));
  415. }
  416. /*}}}  */
  417. /*{{{  total_save*/
  418. public void total_save(FILE * const write_pipe)
  419. {
  420.   pre_top_fold();
  421.   if
  422.    (    (bd.m.file_changed_status!=unchanged_file && !bd.m.read_only)
  423.      || write_pipe
  424.    )
  425.      if
  426.       (    write_fold(get_data(bd.f.real_tail),bd.f.real_head->next,write_pipe)
  427.         && write_pipe==(FILE*)0
  428.       )
  429.       { bd.m.file_changed_status=unchanged_file;
  430.         title_op(CHGTITLE);
  431.       }
  432. }
  433. /*}}}  */
  434.